import {queryCount} from "../services/poet";

export default {

  namespace: 'count',

  state: {
    accessCount: {
      total: 1,
      count: 1,
    }
  },

  subscriptions: {
    setup({dispatch, history}) {  // eslint-disable-line
      dispatch({
        type: 'fetch',
      })
    },
  },

  effects: {
    // 启动时，主动拉取最新的数据
    * fetch({payload}, {call, put}) {  // eslint-disable-line
      const countRes = yield call(queryCount);
      if (countRes.data.status && countRes.data.status.code === 200) {
        yield put({type: 'updateCount', payload: countRes.data.result});
      }
    },
  },

  reducers: {
    updateCount(state, {payload: apps}) {
      return {...state, apps, accessCount: apps};
    }
  },

};
